#!/bin/bash
# date: 2024/05/30
# auth: frealess11

# https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-getting-started.html
# https://hub.docker.com/_/mysql/tags

docker_name=mysql
dir=/data/mysql

docker_image() {
    docker pull mysql:5.7
    docker tag mysql:5.7 hub.lbxcn.com/basic/mysql:5.7
    docker push hub.lbxcn.com/basic/mysql:5.7
}

docker_start() {
    mkdir -p ${dir} &>/dev/null
    docker run --name=${docker_name} \
    -m 1G \
    -p 3306:3306 \
    --restart=always \
    -v ${dir}:/var/lib/mysql \
    -v /var/run/mysqld:/var/run/mysqld \
    -e MYSQL_ROOT_PASSWORD=Rewq@321 \
    -d hub.lbxcn.com/basic/mysql:5.7
}

docker_stop() {
    docker stop ${docker_name}
    docker rm ${docker_name}
}

docker_clean() {
    rm -rf ${dir} /var/run/mysqld
}

docker_logs() {
    docker logs --tail 10 ${docker_name}
}

docker_exec() {
    docker exec -it ${docker_name} mysql -uroot -p'Rewq@321' -A
}

case $1 in
    start)
        docker_start
    ;;
    stop)
        docker_stop
    ;;
    clean)
        docker_clean
    ;;
    logs)
        docker_logs
    ;;
    exec)
        docker_exec
    ;;
    *)
        echo "start|stop|clean|logs|exec"
    ;;
esac
